import Dao.UserDao;
import IOC.InjectionProtety;
import IOC.InjectionSet;
import IOC.P;
import IOC.User;
import Service.UserService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by futao on 2017/8/31.
 */
public class TestCase {

    //region 通过有参构造方法-属性注入
    @Test
    public void testInjectionPropety() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        InjectionProtety ip = (InjectionProtety) applicationContext.getBean("injectionPropety");
        System.out.println(ip.getName());
    }
    //endregion
    //region 通过set -属性注入
    @Test
    public void testInjectionSet() {
        /**
         * 加载配置文件
         */
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        /**
         * 通过配置文件getBean()获得相应的对象
         */
        InjectionSet injectionSet = (InjectionSet) applicationContext.getBean("injectionSet");
        //用这种方式也行  ?
        // InjectionSet injectionSet=applicationContext.getBean(InjectionSet.class);
        /**
         * 调用bean中得到的对象的属性或者方法
         */
        injectionSet.testMethod();
    }
    //endregion

    /**
     * 注入对象
     */
    @Test
    public void testUserService() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService userService = (UserService) applicationContext.getBean("userService");
        userService.add();
    }
    /**
     * 名字空间注入
     */
    @Test
    public void testP(){
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
        P p= (P) applicationContext.getBean("p");
        p.Pp();
    }
    /**
     * 复杂类型的属性值
     */
    @Test
    public void testPpppp(){
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
        P p=(P)applicationContext.getBean("Pp");
        p.Pp();
    }

}
